<?php
class AclInstallationsController extends AppController{
	var $layout = 'controlpanel';
	var $uses = array();
		
	function beforeFilter(){}
	
	function installACL() {
		$this->redirect(array('action' => 'createGroupsAROS'), null, true);
	}
	
	function createGroupsAROS () {
		$aro =& $this->Acl->Aro;
			
		$this->loadModel('TypeUser');
			
		$groupUsers = $this->TypeUser->find('all');
			
		$groups = array();
		$i = 0;
		foreach ($groupUsers as $groupUser) {
			$groups[$i] = array ('alias' => $groupUser['TypeUser']['typeUserName']);
			$i++;
		}
		foreach ($groups as $data) {
			$aro->create();
			$aro->save($data);
		}
		$this->redirect(array('action' => 'addUserToGroups'), null, true);
	}
	function addUserToGroups() {
		$aro = new Aro();
		
		$this->loadModel('User');
		
		$users = $this->User->find('all');
		$usersForAROS = array();
		$i = 0;
		foreach ($users as $user) {
			$typeUserName = $user['TypeUser']['typeUserName'];
			$usersForAROS[$i] = array (
		 		'alias' => $user['User']['userName'],
		 		'parent_id' => $user['TypeUser']['id'],
		 		'model' => 'User'
		 		);

		 		$i++;
		}

		foreach ($usersForAROS as $data) {
			$aro->create();
			$aro->save($data);
		}

		$this->redirect(array('action' => 'createAcos'), null, true);
	}
	function createAcos() {

		$aco = new Aco();

		$lists = array (
			0 => array ('alias' => 'schools'),
			1 => array ('alias' => 'sponsors'),
			2 => array ('alias' => 'subscribers'),
			3 => array ('alias' => 'messages'),
			4 => array ('alias' => 'systemReports'),
			5 => array ('alias' => 'systems'),
			6 => array ('alias' => 'reports'),
		);
		foreach ($lists as $data) {
			$aco->create();
			$aco->save($data);
		}

		$this->redirect(array('action' => 'assignPermission'), null, true);
	}
	function assignPermission() {
		$this->loadModel('TypeUser');
		$groupUsers = $this->TypeUser->find('all');

		$i = 0;
		foreach ($groupUsers as $groupUser) {
			if ($groupUser['TypeUser']['typeUser'] == 1) {
				$user = $groupUser['TypeUser']['typeUserName'];

				$this->log($user);
				//system admin
				$this->Acl->allow($user,'schools');
				$this->Acl->allow($user,'sponsors');
				$this->Acl->allow($user,'subscribers');
				$this->Acl->allow($user,'systemReports');
				$this->Acl->allow($user,'systems');
				//school admin
				$this->Acl->allow($user,'messages');
				$this->Acl->allow($user,'reports');
				
			} else if ($groupUser['TypeUser']['typeUser'] == 2) {
				$user = $groupUser['TypeUser']['typeUserName'];
				$this->log($user);

				$this->Acl->deny($user,'schools');
				$this->Acl->deny($user,'sponsors');
				$this->Acl->deny($user,'subscribers');
				$this->Acl->deny($user,'systemReports');
				$this->Acl->deny($user,'systems');

				$this->Acl->allow($user,'messages');
				$this->Acl->allow($user,'reports');
			}
		}

		$this->redirect(array('action' => 'installed'), null, true);
	}

	function installed() {

	}
}
?>